<?php
class Login extends CI_Controller {
	function index()
	{
		if ( !$this->input->post('username') )
		{
			$data = array(
					'author' => false,
				  ); 
			

		}
		else
		{
				$config = $this->config->item('db_config');
				$this->load->model('Dbmodel', '', $config);

				$this->load->library('encrypt');
				
				$data = array(
					'author' => true,
				  ); 
	


				$cond_member = array('mem_user' => $this->input->post('username'));
				$res_member = $this->Dbmodel->select_where( 'member', $cond_member, 'mem_user, mem_pass, mem_role' );

				$data['login_succ'] = false;
				
				if ($res_member)
				{
						$mem_pass = $this->encrypt->decode($res_member[0]->mem_pass);

						if ( $this->input->post('password') != $mem_pass )
						{
							$data['word'] = "Username Or Password Not Match !!!";
							
						}
						else
						{
			
					/*			$this->load->library('session');

								$sess_member_array = array(
										'member' => $res_member[0]->mem_user,
										'role' => $res_member[0]->mem_role,
									);  

								$this->session->set_userdata($sess_member_array);

								$sess_member = $this->session->userdata('member');

								$sess_member_role = $this->session->userdata('role'); */

			

								  $data['member'] = $res_member[0]->mem_user;
								  $data['role'] = $res_member[0]->mem_role;

				//				print_r($data['member']);
								  
								  $this->load->helper('cookie');

								  $cookie = array(
										'name'   => 'mName',
										'value'  => $data['member'],
										'expire' => 86500,
					//					'domain' => 'localhost',
										'path'   => '/',
					//					'prefix' => 'bk_',
					//					'secure' => TRUE
									);

									$this->input->set_cookie($cookie);

								  $cookie2 = array(
										'name'   => 'm_name',
										'value'  => $this->encrypt->encode($data['member']),
										'expire' => 86500,
				//						'domain' => 'localhost',
										'path'   => '/',
					//					'prefix' => 'bk_',
					//					'secure' => TRUE
									);

									$this->input->set_cookie($cookie2);


									$cookie_role = array(
										'name'   => 'role',
										'value'  => $this->encrypt->encode(strtolower($data['role'])),
										'expire' => 86500,
				//						'domain' => 'localhost',
										'path'   => '/',
					//					'prefix' => 'bk_',
					//					'secure' => TRUE
									);

									$this->input->set_cookie($cookie_role);
			
									$data['login_succ'] = true;
			
					//				echo get_cookie('m_name');
					//				echo '<br>';
					//				echo $this->encrypt->decode(get_cookie('m_name'));

						//			die;
									if ( strtolower($data['role']) == 'customer' )
										header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/');
									else
										header('location:http://'.$_SERVER['HTTP_HOST'].'/hotelbooking/index.php/backend/');

						}

		//				echo $sess_member;
		//				echo "<br>";
		//				echo strtolower($sess_member_role);
				}
				else
				{
					$data['word'] = "Username Or Password Not Match !!!";
				}
		 }
		
	//	print_r($data);
		$this->load->view('loginview', $data);

	}


}
?>